package com.dommettj.rawfix.gui;

import com.dommettj.rawfix.gui.event.GuiEventHandler;
import com.dommettj.rawfix.settings.Settings;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

public class RawFixMenu implements ActionListener {

    public static final String SETTINGS_WINDOW = "SettingsWindow";
    public static final String MENU_BAR = "MenuBar";
    public static final String HOST_FIELD = "HostField";
    public static final String PORT_FIELD = "PortField";
    public static final String SENDER_FIELD = "SenderField";
    public static final String TARGET_FIELD = "TargetField";
    public static final String SAVE_BUTTON = "SaveButton";
    public static final String CANCEL_BUTTON = "CancelButton";
    private final Settings settings;
    private Collection<GuiEventHandler> eventHandlers;
    private JFrame settingsWindow;
    private JMenuBar menuBar;

    public RawFixMenu(Settings settings, Collection<GuiEventHandler> eventHandlers) {
        this.settings = settings;
        this.eventHandlers = eventHandlers;
    }

    public JMenuBar create() {

        createSettingsWindow();

        if(menuBar == null) {

            menuBar = new JMenuBar();
            menuBar.setName(MENU_BAR);

            final JMenu settingsMenu = new JMenu("Settings");
            final JMenuItem settingsMenuItem = new JMenuItem("Change Settings");
            settingsMenuItem.addActionListener(this);
            settingsMenu.add(settingsMenuItem);
            menuBar.add(settingsMenu);
        }
        return menuBar;
    }

    private void createSettingsWindow() {

        if(settingsWindow == null) {

            settingsWindow = new JFrame();
            final JLabel hostLabel = new JLabel("host");
            final JTextField hostField = new JTextField();
            hostField.setName(HOST_FIELD);
            hostField.setText(settings.loadHost());
            hostField.setColumns(25);

            final JLabel portLabel = new JLabel("port");
            final JTextField portField = new JTextField();
            portField.setName(PORT_FIELD);
            portField.setText(settings.loadPort());
            portField.setColumns(25);

            final JLabel senderLabel = new JLabel("senderCompId");
            final JTextField senderField = new JTextField();
            senderField.setName(SENDER_FIELD);
            senderField.setText(settings.loadSenderCompId());
            senderField.setColumns(19);

            final JLabel targetLabel = new JLabel("targetCompId");
            final JTextField targetField = new JTextField();
            targetField.setName(TARGET_FIELD);
            targetField.setText(settings.loadTargetCompId());
            targetField.setColumns(19);

            final JButton saveButton = new JButton("Save");
            saveButton.setName(SAVE_BUTTON);
            saveButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    settings.save(hostField.getText(), portField.getText(), senderField.getText(), targetField.getText());

                    for(final GuiEventHandler handler :eventHandlers) {
                        handler.settingsUpdated(settings);
                    }

                    settingsWindow.setVisible(false);
                }
            });

            final JButton cancelButton = new JButton("Cancel");
            cancelButton.setName(CANCEL_BUTTON);
            cancelButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    settingsWindow.setVisible(false);
                }
            });

            final JPanel settingsPanel = new JPanel();
            settingsPanel.add(hostLabel);
            settingsPanel.add(hostField);
            settingsPanel.add(portLabel);
            settingsPanel.add(portField);
            settingsPanel.add(senderLabel);
            settingsPanel.add(senderField);
            settingsPanel.add(targetLabel);
            settingsPanel.add(targetField);
            settingsPanel.add(saveButton);
            settingsPanel.add(cancelButton);
            settingsPanel.setPreferredSize(new Dimension(350,150));

            settingsWindow.add(settingsPanel);
            settingsWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            settingsWindow.setTitle("Settings");
            settingsWindow.setName(SETTINGS_WINDOW);
            settingsWindow.pack();

            GuiHelper.centreWindow(settingsWindow);
        }
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        settingsWindow.setVisible(true);
    }
}
